package pharao.client.views.game.landkarte.ort.exam;

import java.util.Iterator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import pharao.client.dto.ExamenStatusDTO;
import pharao.client.i18ln.GuiText;
import pharao.client.views.game.landkarte.ort.basics.events.ShowOrtBasicViewEvent;
import pharao.client.views.game.landkarte.ort.exam.events.AddExamenStatusDTOEvent;
import pharao.client.views.game.landkarte.ort.exam.events.ExamNotAvailAbleEvent;
import pharao.client.views.game.landkarte.ort.exam.events.SetExamenEvent;
import pharao.client.views.game.landkarte.ort.lesson.Lesson;
import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.events.CloseViewEvent;
import hmvc4gwt.framework.client.view.GuiView;

public class ExamView extends GuiView {

	/**
	 * prüfen Button
	 */
	private Button buttonCheck = new Button();

	/**
	 * Startbutton
	 */
	private Button buttonStart = new Button();

	/**
	 * enthält die Seite die zz angezeigt wird
	 */
	private int currentPage = 0;

	/**
	 * enthält das Examen
	 */
	private Examen examen;

	/**
	 * container für das Ergebnis
	 */
	private FlowPanel flowPanelResult = new FlowPanel();

	/**
	 * enthält die Navigationselement
	 */
	private HorizontalPanel horizontalPanelNavi = new HorizontalPanel();

	/**
	 * zurück-link
	 */
	private Hyperlink hyperlinkBack = new Hyperlink(GuiText.constants
			.link_back(), null);

	/**
	 * link zur nächsten Seiten
	 */
	private Hyperlink hyperlinkNextPage = new Hyperlink(GuiText.constants
			.link_game_nextPage(), null);

	/**
	 * link zur vorherigen Seite
	 */
	private Hyperlink hyperlinkPrevPage = new Hyperlink(GuiText.constants
			.link_game_prevPage(), null);

	/**
	 * 
	 */
	private Label labelStartNotAvailable = new Label(GuiText.constants
			.text_game_map_ort_exam_startTextNotAvailAble());

	/**
	 * enthält den Einfürungstext
	 */
	private Label labelStartText = new Label(GuiText.constants
			.text_game_map_ort_exam_startText());

	/**
	 * enthält den Titel des Examen
	 */
	private Label labelTitle = new Label();

	/**
	 * enthält die Letzte Seite
	 */
	private int lastPage = 0;

	/**
	 * 
	 */
	private VerticalPanel verticalPanelExamen = new VerticalPanel();
	
	private Examen getExamen() {
		return examen;
	}

	public void handleEventImpl(IHmvcEvent event) {
		if (event instanceof SetExamenEvent) {
			setExamen((Examen) event.getPayload());
		} else if (event instanceof ExamNotAvailAbleEvent) {
			notAvailAble();
		}
	}

	private void handleListenerAction(Widget sender) {
		if (sender == buttonCheck) {
			showResult();
		} else if (sender == hyperlinkNextPage) {
			showNextPage();
		} else if (sender == hyperlinkPrevPage) {
			showPrevPage();
		} else if (sender == hyperlinkBack) {
			getController().fireEventLocal(new CloseViewEvent());
			getController().fireEvent(new ShowOrtBasicViewEvent(), true, false,
					false);
		} else if (sender == buttonStart) {
			remove(hyperlinkBack);
			showPage(0);
		}
	}

	public void initImpl() {
	}

	private void notAvailAble() {

		verticalPanelExamen.clear();
		verticalPanelExamen.add(labelTitle);
		verticalPanelExamen.add(labelStartText);
		verticalPanelExamen.add(labelStartNotAvailable);
		verticalPanelExamen.add(hyperlinkBack);

		clear();
		add(verticalPanelExamen);

	}

	public void pre() {
		getController().register(SetExamenEvent.class, this);
		getController().register(ExamNotAvailAbleEvent.class, this);
		
		labelTitle.setStylePrimaryName("header1");
		labelStartText.setStylePrimaryName("text");
		labelStartNotAvailable.setStylePrimaryName("text");

		/*
		 * Listener für alle Elemente
		 */
		ClickListener listener = new ClickListener() {
			public void onClick(Widget sender) {
				handleListenerAction(sender);
			}
		};

		/*
		 * Listener hinzufügen
		 */
		hyperlinkNextPage.addClickListener(listener);
		hyperlinkPrevPage.addClickListener(listener);
		hyperlinkBack.addClickListener(listener);

		buttonCheck.addClickListener(listener);
		buttonCheck.setText(GuiText.constants
				.button_game_map_ort_exam_checkExam());

		buttonStart.addClickListener(listener);
		buttonStart
				.setText(GuiText.constants.button_game_mapt_ort_exam_start());
	}

	private void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	private void setExamen(Examen examen) {
		this.examen = examen;
		
		/*
		 * letzte Seite berechnen
		 */
		lastPage = (examen.getListLessons().size() / examen.getLessonsPerPage()) - 1;
		
		labelTitle.setText(examen.getTitle());
		
		startExamen();
	}

	private void showNextPage() {
		if (++currentPage >= (lastPage)) {
			currentPage = lastPage;
			showPage(currentPage);
		} else {
			showPage(currentPage);
		}
	}

	private void showPage(int i) {

		setCurrentPage(i);

		/*
		 * aktuelle anzeige löschen
		 */
		remove(verticalPanelExamen);
		verticalPanelExamen.clear();
		horizontalPanelNavi.clear();

		/*
		 * welche Lessons sollen angezeigt werden
		 */
		int min = (i * getExamen().getLessonsPerPage());
		int max = (min + getExamen().getLessonsPerPage());

		int cnt = 0;

		Iterator iterator = getExamen().getListLessons().iterator();
		while (iterator.hasNext()) {
			/*
			 * Aufgaben der Seite hinzufügen
			 */
			if ((cnt >= min) && (cnt < max)) {
				Lesson lesson = (Lesson) iterator.next();
				verticalPanelExamen.add(lesson);
			} else if (cnt >= max) {
				break;
			} else {
				iterator.next();
			}

			cnt++;
		}

		/*
		 * Buttons hinzufügen
		 */
		if (i == 0) {
			horizontalPanelNavi.add(hyperlinkNextPage);
		} else if ((i > 0) && (i < lastPage)) {
			horizontalPanelNavi.add(hyperlinkPrevPage);
			horizontalPanelNavi.add(hyperlinkNextPage);
		} else if (i == lastPage) {
			horizontalPanelNavi.add(hyperlinkPrevPage);
			horizontalPanelNavi.add(buttonCheck);
		}

		verticalPanelExamen.add(horizontalPanelNavi);

		/*
		 * Aufgaben anzeigen
		 */
		add(verticalPanelExamen);
	}

	private void showPrevPage() {
		if (--currentPage <= 0) {
			currentPage = 0;
			showPage(0);
		} else {
			showPage(currentPage);
		}
	}

	private void showResult() {

		getExamen().checkEntrys();

		flowPanelResult.clear();

		Iterator iterator = getExamen().getListLessons().iterator();
		while (iterator.hasNext()) {
			flowPanelResult.add((Lesson) iterator.next());
		}
		
		ExamenStatusDTO examenStatusDTO = getExamen().getExamenStatusDTO();

		remove(verticalPanelExamen);
		add(flowPanelResult);
		add(hyperlinkBack);
		
		getController().fireEventLocal(new AddExamenStatusDTOEvent(examenStatusDTO));
	}

	private void startExamen() {

		verticalPanelExamen.clear();
		verticalPanelExamen.add(labelTitle);
		verticalPanelExamen.add(labelStartText);
		verticalPanelExamen.add(buttonStart);

		clear();
		add(hyperlinkBack);
		add(verticalPanelExamen);

	}

	public void terminate() {
		// TODO Auto-generated method stub

	}
}
